package com.alan.android.sqlite.table;



import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.Map;

/**
 * @author Alan
 * 时 间：2019-11-27
 * 简 述：<功能简述>
 */
public class PatcherHelper {

    private static final String TAG = "PatcherHelper";

    public static <T> void alertTableForAdd(String[] columns, SQLiteDatabase sqLiteDatabase, Class<T> tClass) {
        Table table = TableFactory.getTable(tClass);
        Map<String, Column> columnsMap = table.getColumns();
        for (String column : columns) {
            Column c = columnsMap.get(column);
            if (c != null) {
                addColumn(table.getTableName(), c, sqLiteDatabase);
            }
        }
    }


    private static void addColumn(String tableName, Column column, SQLiteDatabase sqLiteDatabase) {
        String sqlFormat = "ALTER TABLE %s ADD COLUMN %s %s";
        String sql = String.format(sqlFormat, tableName, column.getName(), column.getColumnConverter().getDBType().name());
        Log.d(TAG,"addColumn:"+sql);
        sqLiteDatabase.execSQL(sql);
    }

    public static <T> void createTable(Class<T> tClass, SQLiteDatabase sqLiteDatabase) {
        Table table = TableFactory.getTable(tClass);
        String sql = table.getCreateSqlStr();
        Log.d(TAG,"createTable:"+sql);
        sqLiteDatabase.execSQL(sql);
    }
}
